﻿//==================================================================
// Copyright (C) 2011 深圳市中旭企业管理股份有限公司
// 文件名: bpm_NeedsOfBorrower.cs
// 作 者：代码自动生成
// 日 期：2012/6/18 14:36:45
// 描 述：
// 版 本：1.00
// 修改历史纪录
// 版 本  修改时间      修改人            修改内容
// 1.00             
//==================================================================
using System;
using System.ComponentModel;
using System.Data.SqlClient;
using Csla;
using Csla.Data;
using Csla.Validation;
using lite;
using OpenExpressApp.ORM;
using ZX.DAL;

namespace ZX.BPM.Library
{
    /// <summary>
    /// 需求、借款、报销单
    /// </summary>
    [Serializable]
    [Table(Name = "bpm_NeedsOfBorrower")]
    [ZX.DBModule.Table("bpm_NeedsOfBorrower", "ID")]
    public class NeedsOfBorrowerEdit : BusinessBase<NeedsOfBorrowerEdit>
    {

        #region Business Methods 业务方法

        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<Guid> IDProperty = RegisterProperty(typeof(NeedsOfBorrowerEdit), new PropertyInfo<Guid>("ID"));
        [DataObjectField(true, true)]
        [Column, PK]
        public Guid ID
        {
            get { return GetProperty(IDProperty); }
            private  set { SetProperty(IDProperty, value); }
        }

        /// <summary>
        /// 物品采购类型(物品：G 非物品：N)
        /// </summary>
        private static PropertyInfo<string> ApplyTypeProperty = RegisterProperty(typeof(NeedsOfBorrowerEdit), new PropertyInfo<string>("ApplyType"));
         [Column]
        public string ApplyType
        {
            get { return GetProperty(ApplyTypeProperty); }
            set { SetProperty(ApplyTypeProperty, value); }
        }
        /// <summary>
        /// 物品采购、需求总金额
        /// </summary>
        private static PropertyInfo<decimal> DemandTotalAmountProperty = RegisterProperty(typeof(NeedsOfBorrowerEdit), new PropertyInfo<decimal>("DemandTotalAmount"));
         [Column]
        public decimal DemandTotalAmount
        {
            get { return GetProperty(DemandTotalAmountProperty); }
            set { SetProperty(DemandTotalAmountProperty, value); }
        }
        /// <summary>
        /// 借款类型(无需借款：not;现金：Cash；银行付款：Bank)
        /// </summary>
        private static PropertyInfo<string> LoanTypeProperty = RegisterProperty(typeof(NeedsOfBorrowerEdit), new PropertyInfo<string>("LoanType"));
         [Column]
        public string LoanType
        {
            get { return GetProperty(LoanTypeProperty); }
            set { SetProperty(LoanTypeProperty, value); }
        }
        /// <summary>
        /// 申请人借款金额
        /// </summary>
        private static PropertyInfo<decimal> LoanAmountProperty = RegisterProperty(typeof(NeedsOfBorrowerEdit), new PropertyInfo<decimal>("LoanAmount"));
         [Column]
        public decimal LoanAmount
        {
            get { return GetProperty(LoanAmountProperty); }
            set { SetProperty(LoanAmountProperty, value); }
        }
        /// <summary>
        /// 备注
        /// </summary>
        private static PropertyInfo<string> RemarksProperty = RegisterProperty(typeof(NeedsOfBorrowerEdit), new PropertyInfo<string>("Remarks"));
         [Column]
        public string Remarks
        {
            get { return GetProperty(RemarksProperty); }
            set { SetProperty(RemarksProperty, value); }
        }
        /// <summary>
        /// 采购借款方式(无需借款：not;现金：Cash；银行付款：Bank)
        /// </summary>
        private static PropertyInfo<string> ProcurementLoanBasisProperty = RegisterProperty(typeof(NeedsOfBorrowerEdit), new PropertyInfo<string>("ProcurementLoanBasis"));
         [Column]
        public string ProcurementLoanBasis
        {
            get { return GetProperty(ProcurementLoanBasisProperty); }
            set { SetProperty(ProcurementLoanBasisProperty, value); }
        }
        /// <summary>
        /// 采购借款金额
        /// </summary>
        private static PropertyInfo<decimal> ProcurementLoanAmountProperty = RegisterProperty(typeof(NeedsOfBorrowerEdit), new PropertyInfo<decimal>("ProcurementLoanAmount"));
         [Column]
        public decimal ProcurementLoanAmount
        {
            get { return GetProperty(ProcurementLoanAmountProperty); }
            set { SetProperty(ProcurementLoanAmountProperty, value); }
        }
        /// <summary>
        /// 费用报销类型(一次性报销：First、多次报销：Repeatedly、最后一次报销：Last)
        /// </summary>
        private static PropertyInfo<string> ReimbursementTypeProperty = RegisterProperty(typeof(NeedsOfBorrowerEdit), new PropertyInfo<string>("ReimbursementType"));
         [Column]
        public string ReimbursementType
        {
            get { return GetProperty(ReimbursementTypeProperty); }
            set { SetProperty(ReimbursementTypeProperty, value); }
        }
        /// <summary>
        /// 费用报销总金额
        /// </summary>
        private static PropertyInfo<decimal> ReimbursementTotalAmountProperty = RegisterProperty(typeof(NeedsOfBorrowerEdit), new PropertyInfo<decimal>("ReimbursementTotalAmount"));
         [Column]
        public decimal ReimbursementTotalAmount
        {
            get { return GetProperty(ReimbursementTotalAmountProperty); }
            set { SetProperty(ReimbursementTotalAmountProperty, value); }
        }
        /// <summary>
        /// 申请人
        /// </summary>
        private static PropertyInfo<string> ProposerProperty = RegisterProperty(typeof(NeedsOfBorrowerEdit), new PropertyInfo<string>("Proposer"));
         [Column]
        public string Proposer
        {
            get { return GetProperty(ProposerProperty); }
            set { SetProperty(ProposerProperty, value); }
        }
        /// <summary>
        /// 所属分公司
        /// </summary>
        private static PropertyInfo<string> FilialeProperty = RegisterProperty(typeof(NeedsOfBorrowerEdit), new PropertyInfo<string>("Filiale"));
         [Column]
        public string Filiale
        {
            get { return GetProperty(FilialeProperty); }
            set { SetProperty(FilialeProperty, value); }
        }
        /// <summary>
        /// 所属部门
        /// </summary>
        private static PropertyInfo<string> DeptNameProperty = RegisterProperty(typeof(NeedsOfBorrowerEdit), new PropertyInfo<string>("DeptName"));
         [Column]
        public string DeptName
        {
            get { return GetProperty(DeptNameProperty); }
            set { SetProperty(DeptNameProperty, value); }
        }
        /// <summary>
        /// 所属岗位
        /// </summary>
        private static PropertyInfo<string> PostNameProperty = RegisterProperty(typeof(NeedsOfBorrowerEdit), new PropertyInfo<string>("PostName"));
         [Column]
        public string PostName
        {
            get { return GetProperty(PostNameProperty); }
            set { SetProperty(PostNameProperty, value); }
        }

        /// <summary>
        /// 单号
        /// </summary>
        private static PropertyInfo<string> NoProperty = RegisterProperty(typeof(NeedsOfBorrowerEdit), new PropertyInfo<string>("No"));
         [Column]
        public string No
        {
            get { return GetProperty(NoProperty); }
            set { SetProperty(NoProperty, value); }
        }

        /// <summary>
        /// 申请时间
        /// </summary>
        private static PropertyInfo<string> CreatedTimeProperty = RegisterProperty(typeof(NeedsOfBorrowerEdit), new PropertyInfo<string>("CreatedTime"));
         [Column]
        public string CreatedTime
        {
            get { return GetProperty(CreatedTimeProperty); }
            set { SetProperty(CreatedTimeProperty, value); }
        }
        /// <summary>
        /// 流程模板ID
        /// </summary>
        private static PropertyInfo<string> WorkFlowIdProperty = RegisterProperty(typeof(NeedsOfBorrowerEdit), new PropertyInfo<string>("WorkFlowId"));
         [Column]
        public string WorkFlowId
        {
            get { return GetProperty(WorkFlowIdProperty); }
            set { SetProperty(WorkFlowIdProperty, value); }
        }
        /// <summary>
        /// 流程实例ID
        /// </summary>
        private static PropertyInfo<string> WorkFlowInsIdProperty = RegisterProperty(typeof(NeedsOfBorrowerEdit), new PropertyInfo<string>("WorkFlowInsId"));
         [Column]
        public string WorkFlowInsId
        {
            get { return GetProperty(WorkFlowInsIdProperty); }
            set { SetProperty(WorkFlowInsIdProperty, value); }
        }
        /// <summary>
        /// 流程模板任务ID
        /// </summary>
        private static PropertyInfo<string> WorkTaskIdProperty = RegisterProperty(typeof(NeedsOfBorrowerEdit), new PropertyInfo<string>("WorkTaskId"));
         [Column]
        public string WorkTaskId
        {
            get { return GetProperty(WorkTaskIdProperty); }
            set { SetProperty(WorkTaskIdProperty, value); }
        }
        /// <summary>
        /// 流程任务实例ID
        /// </summary>
        private static PropertyInfo<string> WorkTaskInsIdProperty = RegisterProperty(typeof(NeedsOfBorrowerEdit), new PropertyInfo<string>("WorkTaskInsId"));
         [Column]
        public string WorkTaskInsId
        {
            get { return GetProperty(WorkTaskInsIdProperty); }
            set { SetProperty(WorkTaskInsIdProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> ReimbursementModeProperty = RegisterProperty(typeof(NeedsOfBorrowerEdit), new PropertyInfo<string>("ReimbursementMode"));
         [Column]
        public string ReimbursementMode
        {
            get { return GetProperty(ReimbursementModeProperty); }
            set { SetProperty(ReimbursementModeProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> SubjectProperty = RegisterProperty(typeof(NeedsOfBorrowerEdit), new PropertyInfo<string>("Subject"));
         [Column]
        public string Subject
        {
            get { return GetProperty(SubjectProperty); }
            set { SetProperty(SubjectProperty, value); }
        }

        private static readonly PropertyInfo<MaterialsProcurements> MaterialsProcurementsProperty = RegisterProperty<MaterialsProcurements>(p => p.MaterialsProcurements);
        public MaterialsProcurements MaterialsProcurements
        {
            get
            {
                if (!(FieldManager.FieldExists(MaterialsProcurementsProperty)))
                    LoadProperty(MaterialsProcurementsProperty, MaterialsProcurements.NewMaterialsProcurements());
                return GetProperty(MaterialsProcurementsProperty);
            }
            private set { LoadProperty(MaterialsProcurementsProperty, value); }
        }

        private static readonly PropertyInfo<ReimbursementExpenses> ReimbursementExpensesProperty = RegisterProperty<ReimbursementExpenses>(p => p.ReimbursementExpenses);
        public ReimbursementExpenses ReimbursementExpenses
        {
            get
            {
                if (!(FieldManager.FieldExists(ReimbursementExpensesProperty)))
                    LoadProperty(ReimbursementExpensesProperty, ReimbursementExpenses.NewReimbursementExpenses());
                return GetProperty(ReimbursementExpensesProperty);
            }
           private set { LoadProperty(ReimbursementExpensesProperty, value); }
        }

        public override string ToString()
        {
            return ID.ToString();
        }

        #endregion

        #region Validation Rules 验证规则
        //将验证规则与业务对象的属性联系在一起
        protected override void AddBusinessRules()
        {
            //AddRule的第一个参数是委托变量，也就是说是方法的变量
            //ValidationRules.AddRule(CommonRules.StringRequired, "ApplyType");
            //ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("ApplyType", 3));
            //ValidationRules.AddRule(CommonRules.StringRequired, "LoanType");
            //ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("LoanType", 10));
            //ValidationRules.AddRule(CommonRules.StringRequired, "Remarks");
            //ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("Remarks", 400));
            //ValidationRules.AddRule(CommonRules.StringRequired, "ProcurementLoanBasis");
            //ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("ProcurementLoanBasis", 10));
            //ValidationRules.AddRule(CommonRules.StringRequired, "ReimbursementType");
            //ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("ReimbursementType", 10));
            //ValidationRules.AddRule(CommonRules.StringRequired, "Proposer");
            //ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("Proposer", 50));
            //ValidationRules.AddRule(CommonRules.StringRequired, "Filiale");
            //ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("Filiale", 50));
            //ValidationRules.AddRule(CommonRules.StringRequired, "DeptName");
            //ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("DeptName", 50));
            //ValidationRules.AddRule(CommonRules.StringRequired, "PostName");
            //ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("PostName", 50));
            //ValidationRules.AddRule(CommonRules.StringRequired, "CreatedTime");
            //ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("CreatedTime", 50));
            //ValidationRules.AddRule(CommonRules.StringRequired, "WorkFlowId");
            //ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("WorkFlowId", 50));
            //ValidationRules.AddRule(CommonRules.StringRequired, "WorkFlowInsId");
            //ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("WorkFlowInsId", 50));
            //ValidationRules.AddRule(CommonRules.StringRequired, "WorkTaskId");
            //ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("WorkTaskId", 50));
            //ValidationRules.AddRule(CommonRules.StringRequired, "WorkTaskInsId");
            //ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("WorkTaskInsId", 50));
            //ValidationRules.AddRule(CommonRules.StringRequired, "ReimbursementMode");
            //ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("ReimbursementMode", 10));
            //ValidationRules.AddRule(CommonRules.StringRequired, "Subject");
            //ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("Subject", 100));
            base.AddBusinessRules();
        }
        #endregion


   
        #region Factory Methods 工厂方法

        private NeedsOfBorrowerEdit()
        {

        }

        public static NeedsOfBorrowerEdit New()
        {
            return DataPortal.Create<NeedsOfBorrowerEdit>();
        }
        
        public static NeedsOfBorrowerEdit Get(Guid ID)
        {
            return DataPortal.Fetch<NeedsOfBorrowerEdit>(new SingleLazyCriteria<NeedsOfBorrowerEdit, Guid>(ID, false));
        }

        public static NeedsOfBorrowerEdit Get(string worktaskInsID)
        {
            return DataPortal.Fetch<NeedsOfBorrowerEdit>(worktaskInsID);
        }

        public static void Delete(Guid ID)
        {
            DataPortal.Delete(ID);
        }


        public void InsertMaterialsProcurement(MaterialsProcurement materialsProcurement)
        {
            this.MaterialsProcurements.Add(materialsProcurement);
        }

        public void UpdateMaterialsProcurement(MaterialsProcurement materialsProcurement)
        {
            this.MaterialsProcurements.Update(materialsProcurement);
        }

        public void DeleteMaterialsProcurement(MaterialsProcurement materialsProcurement)
        {
            this.MaterialsProcurements.Remove(materialsProcurement.ID);
        }


        public MaterialsProcurements GetMaterialsProcurements()
        {
            return this.MaterialsProcurements;
        }


        public void InsertReimbursementExpense(ReimbursementExpense reimbursementExpense)
        {

            this.ReimbursementExpenses.Add(reimbursementExpense);

        }

        public void UpdateReimbursementExpense(ReimbursementExpense reimbursementExpense)
        {

            this.ReimbursementExpenses.Update(reimbursementExpense);

        }

        public void DeleteReimbursementExpense(ReimbursementExpense reimbursementExpense)
        {
            this.ReimbursementExpenses.Remove(reimbursementExpense.ID);
        }


        public ReimbursementExpenses GetReimbursementExpenses()
        {
            return this.ReimbursementExpenses;
        }
        #endregion

        protected override void OnChildChanged(Csla.Core.ChildChangedEventArgs e)
        {
            //if (e.ChildObject is MaterialsProcurement)
            //{
            //    OnPropertyChanged("MaterialsProcurement");
            //}

            //if (e.ChildObject is ReimbursementExpense)
            //{
            //    OnPropertyChanged("ReimbursementExpense");
            //}

            base.OnChildChanged(e);
        }

        #region Data Access 数据访问
        //由于DataPortal_create中没有对数据库的访问，所有标记了Runlocal特性，使得该方法只需在本地运行即可
        //如果代码中有访问数据库的方法，那么就不要用这个特性，使数据门户可以根据情况将此方法的运行选择在应用服务器还是本地
        [RunLocal()]
        protected override void DataPortal_Create()
        {
            //在此调用CheckRules对所有的属性进行验证规则的检查，这样检查只运行一次。
            LoadProperty(IDProperty, Guid.NewGuid());
            LoadProperty(CreatedTimeProperty, DateTime.Now.ToString());
            LoadProperty(DeptNameProperty, string.Empty);
            LoadProperty(ProposerProperty, string.Empty);
            LoadProperty(PostNameProperty, string.Empty);
            LoadProperty(FilialeProperty, string.Empty);
            LoadProperty(ReimbursementTotalAmountProperty, 0);
            LoadProperty(DemandTotalAmountProperty, 0);
            LoadProperty(ProcurementLoanAmountProperty, 0);

            base.DataPortal_Create();
        }


        //可以看到此方法没有标记任何特性
        //此方法要访问数据库所以不能标记本地，由于她没有任何数据库更新操作所以也无需事务性的保护
        //此方法调用结束后会隐含的调用MarkOld（）方法
        private void DataPortal_Fetch(Guid ID)
        {
            //通过连接字符串建立SQL连接对象，Using符号代表，范围释放的意思，SqlConnection对象会在此范围后被释放掉
            using (var ctx = ConnectionManager<SqlConnection>.GetManager(ConnectionStringNames.OpenExpressApp))
            {
                IDb db = DbFactory.Instance.GetDb(ctx.Connection);
                IQuery q = db.Query();
                q.Constrain("ID").Equal(ID);
                var data = db.Select<NeedsOfBorrowerEdit>(q)[0];
                LoadProperty(IDProperty, data.ID);
                LoadProperty(ApplyTypeProperty, data.ApplyType);
                LoadProperty(DemandTotalAmountProperty, data.DemandTotalAmount);
                LoadProperty(LoanTypeProperty, data.LoanType);
                LoadProperty(LoanAmountProperty, data.LoanAmount);
                LoadProperty(RemarksProperty, data.Remarks);
                LoadProperty(ProcurementLoanBasisProperty, data.ProcurementLoanBasis);
                LoadProperty(ProcurementLoanAmountProperty, data.ProcurementLoanAmount);
                LoadProperty(ReimbursementTypeProperty, data.ReimbursementType);
                LoadProperty(ReimbursementTotalAmountProperty, data.ReimbursementTotalAmount);
                LoadProperty(ProposerProperty, data.Proposer);
                LoadProperty(FilialeProperty, data.Filiale);
                LoadProperty(DeptNameProperty, data.DeptName);
                LoadProperty(PostNameProperty, data.PostName);
                LoadProperty(CreatedTimeProperty, data.CreatedTime);
                LoadProperty(WorkFlowIdProperty, data.WorkFlowId);
                LoadProperty(WorkFlowInsIdProperty, data.WorkFlowInsId);
                LoadProperty(WorkTaskIdProperty, data.WorkTaskId);
                LoadProperty(WorkTaskInsIdProperty, data.WorkTaskInsId);
                LoadProperty(ReimbursementModeProperty, data.ReimbursementMode);
                LoadProperty(SubjectProperty, data.Subject);
                LoadProperty(NoProperty, data.No);

                LoadProperty(MaterialsProcurementsProperty, DataPortal.FetchChild<MaterialsProcurements>(ID));
                LoadProperty(ReimbursementExpensesProperty, DataPortal.FetchChild<ReimbursementExpenses>(ID));
            }
        }

        //可以看到此方法没有标记任何特性
        //此方法要访问数据库所以不能标记本地，由于她没有任何数据库更新操作所以也无需事务性的保护
        //此方法调用结束后会隐含的调用MarkOld（）方法
        private void DataPortal_Fetch(string workTaskInsId)
        {
            //通过连接字符串建立SQL连接对象，Using符号代表，范围释放的意思，SqlConnection对象会在此范围后被释放掉
            using (var ctx = ConnectionManager<SqlConnection>.GetManager(ConnectionStringNames.OpenExpressApp))
            {
                IDb db = DbFactory.Instance.GetDb(ctx.Connection);
                IQuery q = db.Query();
                q.Constrain("WorkFlowInsId").Equal(workTaskInsId);
                var data = db.Select<NeedsOfBorrowerEdit>(q)[0];
                LoadProperty(IDProperty, data.ID);
                LoadProperty(ApplyTypeProperty, data.ApplyType);
                LoadProperty(DemandTotalAmountProperty, data.DemandTotalAmount);
                LoadProperty(LoanTypeProperty, data.LoanType);
                LoadProperty(LoanAmountProperty, data.LoanAmount);
                LoadProperty(RemarksProperty, data.Remarks);
                LoadProperty(ProcurementLoanBasisProperty, data.ProcurementLoanBasis);
                LoadProperty(ProcurementLoanAmountProperty, data.ProcurementLoanAmount);
                LoadProperty(ReimbursementTypeProperty, data.ReimbursementType);
                LoadProperty(ReimbursementTotalAmountProperty, data.ReimbursementTotalAmount);
                LoadProperty(ProposerProperty, data.Proposer);
                LoadProperty(FilialeProperty, data.Filiale);
                LoadProperty(DeptNameProperty, data.DeptName);
                LoadProperty(PostNameProperty, data.PostName);
                LoadProperty(CreatedTimeProperty, data.CreatedTime);
                LoadProperty(WorkFlowIdProperty, data.WorkFlowId);
                LoadProperty(WorkFlowInsIdProperty, data.WorkFlowInsId);
                LoadProperty(WorkTaskIdProperty, data.WorkTaskId);
                LoadProperty(WorkTaskInsIdProperty, data.WorkTaskInsId);
                LoadProperty(ReimbursementModeProperty, data.ReimbursementMode);
                LoadProperty(SubjectProperty, data.Subject);
                LoadProperty(NoProperty, data.No);

                LoadProperty(MaterialsProcurementsProperty, DataPortal.FetchChild<MaterialsProcurements>(ID));
                LoadProperty(ReimbursementExpensesProperty, DataPortal.FetchChild<ReimbursementExpenses>(ID));
            }
        }

        protected override void DataPortal_Insert()
        {
            using (var ctx = ConnectionManager<SqlConnection>.GetManager(ZX.DAL.ConnectionStringNames.OpenExpressApp))
            {
                IDb db = DbFactory.Instance.GetDb(ctx.Connection);
                db.Insert(this);

                FieldManager.UpdateChildren(this);
            }

        
           
        }

        protected override void DataPortal_Update()
        {

            using (var ctx = ConnectionManager<SqlConnection>.GetManager(ZX.DAL.ConnectionStringNames.OpenExpressApp))
            {
                IDb db = DbFactory.Instance.GetDb(ctx.Connection);
                db.Update(this);

                FieldManager.UpdateChildren(this);
            }

        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_DeleteSelf()
        {
            //依然是调用下面的方法删除的，只是他此时可以获得自己的Id
            DataPortal_Delete(this.ID);
        }

        private void DataPortal_Delete(Guid id)
        {
            using (var ctx = ConnectionManager<SqlConnection>.GetManager(ConnectionStringNames.OpenExpressApp))
            {
                MaterialsProcurements.Clear();
                FieldManager.UpdateChildren(this);


                IDb db = DbFactory.Instance.GetDb(ctx.Connection);
                db.Delete(this);
            }
        }
        #endregion

    }
}

